﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_PayBack")]
    public class P_PayBack : IEntity<P_PayBack>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        public int ProjectID { get; set; }
        public int PolicyID { get; set; }
        public int InvestmentID { get; set; }
        public int RepaymentPlanItemID { get; set; }
        public decimal Rate { get; set; }
        public decimal PayBackInterest { get; set; }
        public decimal PayBackPrincipal { get; set; }
        public decimal PayBackedInterest { get; set; }
        public decimal PayBackedPrincipal { get; set; }
        public int CycleAmount { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime CreateTime { get; set; }
        public int Status { get; set; }

        public override P_PayBack Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_PayBack Find(System.Linq.Expressions.Expression<Func<P_PayBack, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PayBack.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CycleAmount = item.CycleAmount;
                    this.EndDate = item.EndDate;
                    this.ID = item.ID;
                    this.InvestmentID = item.InvestmentID;
                    this.PayBackedInterest = item.PayBackedInterest;
                    this.PayBackedPrincipal = item.PayBackedPrincipal;
                    this.PayBackInterest = item.PayBackInterest;
                    this.PayBackPrincipal = item.PayBackPrincipal;
                    this.PolicyID = item.PolicyID;
                    this.ProjectID = item.ProjectID;
                    this.Rate = item.Rate;
                    this.RepaymentPlanItemID = item.RepaymentPlanItemID;
                    this.StartDate = item.StartDate;
                    this.Status = item.Status;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_PayBack, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PayBack.Any(where);
            }
        }
    }
}